/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Business;

import Objects.Permission;
import Objects.Timework;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nguyen Kim Manh
 */
public class PermissionBusiness {

    /**
     * Method getPermissionById() used to get Permission by PermissionID
     *
     * @param variable has string type
     * @return objPermission
     */
    public static Permission getPermissionById(String PermissionID) {
        Permission per = new Permission();

        String sql = "{call sp_getPermissionByID(?)}";
        ArrayList list = new ArrayList();
        list.add(PermissionID);
        try {
            ResultSet rs = DBAccess.DB_Connect.executeQuery(sql, list);
            if (rs.next()) {
                per.setPermissionID(rs.getString("PermissionID"));
                per.setPermissionDes(rs.getString("Description"));
            }


        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TimeworkBusiness.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TimeworkBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }

        return per;
    }

    /**
     * method used to update Permission object
     */
    public static int updatePermission(String perID, String perDes) {
        int kq = 0;

        String sql = "{call sp_updatePermission(?,?)}";
        ArrayList list = new ArrayList();
        list.add(perID);
        list.add(perDes);
        try {
            kq = DBAccess.DB_Connect.executeNonQuery(sql, list);


        } catch (SQLException ex) {
            Logger.getLogger(PermissionBusiness.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PermissionBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }

        return kq;
    }

    /**
     * Method getPermissionByDesciption() used to get Permission by Description
     * Field
     *
     * @param variable has string type
     * @return objPermission
     */
    public static Permission getPermissionByDes(String PerDes) {
        Permission per = new Permission();

        String sql = "{call sp_getPermissionByDescription(?)}";
        ArrayList list = new ArrayList();
        list.add(PerDes);
        try {
            ResultSet rs = DBAccess.DB_Connect.executeQuery(sql, list);
            if (rs.next()) {
                per.setPermissionID(rs.getString("PermissionID"));
                per.setPermissionDes(rs.getString("Description"));
            }


        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TimeworkBusiness.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TimeworkBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }

        return per;
    }

    /**
     * Method getAllPermission() used to get all permission information from
     * database
     *
     * @return ArrayList<Timework>
     */
    public static ArrayList<Permission> getAllPermission() {
        ArrayList<Permission> list = new ArrayList<>();

        String sql = "{call sp_getAllPermission}";
        try {
            ResultSet rs = DBAccess.DB_Connect.executeQuery(sql, null);
            while (rs.next()) {
                Permission per = new Permission();
                per.setPermissionID(rs.getString("PermissionID"));
                per.setPermissionDes(rs.getString("Description"));

                list.add(per);

            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TimeworkBusiness.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TimeworkBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }

        return list;
    }

    /**
     * method used to get newest Permission object
     */
    public static Permission getNewestPermission() {
        Permission per = null;

        String sql = "{call sp_getNewestPermission}";
        try {
            ResultSet rs = DBAccess.DB_Connect.executeQuery(sql, null);
            if (rs.next()) {
                per = new Permission();
                per.setPermissionID(rs.getString("PermissionID"));
                per.setPermissionDes(rs.getString("Description"));
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PermissionBusiness.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PermissionBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }


        return per;
    }

    /**
     * method check the existence of Description field
     */
    public static int checkExistence(String perDes) {
        int kt = 0;

        String sql = "{call sp_getPermissionByDescription(?)}";
        ArrayList list = new ArrayList();
        list.add(perDes);
        try {
            ResultSet rs = DBAccess.DB_Connect.executeQuery(sql, list);
            if (rs.next()) {
                kt = 1;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PermissionBusiness.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PermissionBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }


        return kt;
    }

    /**
     * method add new permission
     */
    public static boolean addNewPermission(String perID, String perDes) {
        boolean kt = false;

        String sql = "{call sp_addPermission(?,?)}";
        ArrayList list = new ArrayList();
        list.add(perID);
        list.add(perDes);

        int rs;
        try {
            rs = DBAccess.DB_Connect.executeNonQuery(sql, list);
            if (rs > 0) {
                kt = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(PermissionBusiness.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PermissionBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }


        return kt;
    }
}
